package com.llb666.java.thread.threadpool.demo;


import java.util.concurrent.*;

/**
 * @author mac
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        Future<String> future = newCachedThreadPool.submit(new PoolCallable());
        if(!future.isDone()){
            System.out.println("task has not finished, please wait!");
        }
        try {
            System.out.println(future.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            newCachedThreadPool.shutdown();
        }
    }
    /**
     * 如何实现处理线程的返回值？
     * 通过Callable接口实现：通过FutureTask或者线程池获取
     * @author mac
     */
    static class PoolCallable implements Callable<String> {
        @Override
        public String call() throws Exception{
            String value="test";
            System.out.println("Ready to work");
            Thread.sleep(5000);
            System.out.println("task done");
            return  value;
        }
    }
}
